<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="组织架构"></el-tab-pane>
        </el-tabs>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="bank" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')"
                      >添加子部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!-- 
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon
                  v-if="scope.data.children.length !== 0"
                  :icon-class="scope.node.expanded ? 'zhedie' : 'zhankai'"
                />
                <svg-icon v-else icon-class="person1" ref="menuIcon" />
                <!-- <span>{{ scope.node }}</span> -->
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="doAdd(scope.data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="doEdit(scope.data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          @click.native="confirmDel(scope.data.id)"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <deptDialog
        ref="deptDialogRef"
        :add-dept-pid="addDeptPid"
        :is-edit="isEdit"
        :origin-list="originList"
        @doSuccess="
          showDialog = false;
          loadDepartments();
        "
        @doClose="doClose"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from "@/api/departments";
import { arrayToTree } from "@/utils/index";
import deptDialog from "./deptDialog.vue";

export default {
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      addDeptPid: "",
      isEdit: false,
      originList: [],
    };
  },
  components: { deptDialog },
  created() {
    this.loadDepartments();
  },

  methods: {
    async loadDepartments() {
      const res = await getDepartments();
      console.log(res);
      // res.data.depts.splice(0, 1);
      this.list = [...arrayToTree(res.data.depts, "")];
      this.originList = res.data.depts.map((item) => {
        return {
          name: item.name,
          pid: item.pid,
          code: item.code,
          id: item.id,
        };
      });
    },
    doAdd(id) {
      this.showDialog = true;
      this.isEdit = false;
      console.log(id);
      this.addDeptPid = id;
    },
    doEdit(id) {
      this.showDialog = true;
      // console.log(scope);
      this.isEdit = true;
      this.addDeptPid = id;
      this.$nextTick(() => {
        this.$refs.deptDialogRef.loadDepartmentById(id);
      });
    },
    async doDel(id) {
      // 调用接口删除
      await delDepartment(id);
      // 重发请求,更新数据
      this.loadDepartments();
    },
    confirmDel(id) {
      // 确认删除
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.doDel(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    doClose() {
      this.showDialog = false;
    },
  },
};
</script>

<style>
.el-tree-node__expand-icon {
  /* color: #fff; */
  display: none;
}
.svg-icon {
  margin-right: 10px;
}
</style>
